#pragma once
#include "Common.h"
#include "ObjectPool.h"
#include"PageMap.h"

class PageCache
{
private:
	SpanList _spanLists[NPAGES];						//hash桶
	ObjectPool<Span> _spanPool;							//定长内存池替换new
	//std::unordered_map<PAGE_ID, Span*> _idSpanMap;	//记录PAGE_ID与Span的映射关系
	TCMalloc_PageMap1<32 - PAGE_SHIFT> _idSpanMap;

	static PageCache _sInst;							//单例
	PageCache() {}
	PageCache(const PageCache&) = delete;

public:
	std::mutex _pageMtx;								//全局锁
	static PageCache* GetInstance() { return &_sInst; }

	// 获取从对象到span的映射
	Span* MapObjectToSpan(void* obj);

	// 释放空闲span回到Pagecache，并合并相邻的span
	void ReleaseSpanToPageCache(Span* span);

	// 获取一个K页的span
	Span* NewSpan(size_t k);
};